home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / wizard / ListToListPanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-20  |  9.0 KB  |  312 lines

  1. package asp.wizard;
  2.  
  3. import asp.netobjects.nfx.ui.AwtUtil;
  4. import asp.netobjects.nfx.ui.OrderedListModel;
  5. import com.sun.java.swing.BorderFactory;
  6. import com.sun.java.swing.DefaultListModel;
  7. import com.sun.java.swing.JButton;
  8. import com.sun.java.swing.JLabel;
  9. import com.sun.java.swing.JList;
  10. import com.sun.java.swing.JPanel;
  11. import com.sun.java.swing.JScrollPane;
  12. import com.sun.java.swing.ListModel;
  13. import java.awt.Container;
  14. import java.awt.GridBagConstraints;
  15. import java.awt.GridBagLayout;
  16. import java.awt.Insets;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ComponentEvent;
  19. import java.awt.event.MouseEvent;
  20. import java.lang.reflect.Array;
  21. import java.util.Enumeration;
  22. import java.util.TooManyListenersException;
  23.  
  24. public class ListToListPanel extends JPanel {
  25.    private JLabel _lblLeftList = new JLabel();
  26.    private JLabel _lblRightList = new JLabel();
  27.    private JList _lstLeft = new JList();
  28.    private JList _lstRight = new JList();
  29.    private JButton _btnToLeft = new JButton("<");
  30.    private JButton _btnToRight = new JButton(">");
  31.    private JScrollPane _scpLeft = new JScrollPane();
  32.    private JScrollPane _scpRight = new JScrollPane();
  33.    private boolean _moveSrcItems = false;
  34.    private JButton _btnAllToLeft = new JButton("<<");
  35.    private JButton _btnAllToRight = new JButton(">>");
  36.    private ListToListXferItemListener _xferItemListener;
  37.  
  38.    public ListToListPanel() {
  39.       this.initModels();
  40.       this.initLayout();
  41.       this.initComponentListeners();
  42.    }
  43.  
  44.    public ListToListPanel(OrderedListModel modelLeft, String titleLeft, OrderedListModel modelRight, String titleRight) {
  45.       this._lblLeftList.setText(titleLeft);
  46.       this._lblRightList.setText(titleRight);
  47.       this._lstLeft.setModel(modelLeft);
  48.       this._lstRight.setModel(modelRight);
  49.       this.initLayout();
  50.       this.initComponentListeners();
  51.    }
  52.  
  53.    protected void initModels() {
  54.       this._lstLeft.setModel(new OrderedListModel());
  55.       this._lstRight.setModel(new OrderedListModel());
  56.    }
  57.  
  58.    protected void initLayout() {
  59.       GridBagLayout gbl = new GridBagLayout();
  60.       GridBagConstraints gbc = new GridBagConstraints();
  61.       ((Container)this).setLayout(gbl);
  62.       gbc.fill = 2;
  63.       AwtUtil.addComponent(this, this._lblLeftList, gbl, gbc, 0, 0, 1, 1, (double)0.0F, (double)0.0F);
  64.       gbc.fill = 2;
  65.       AwtUtil.addComponent(this, this._lblRightList, gbl, gbc, 2, 0, 1, 1, (double)1.0F, (double)0.0F);
  66.       gbc.fill = 1;
  67.       this._lstLeft.setFixedCellWidth(100);
  68.       this._scpLeft.setBorder(BorderFactory.createLoweredBevelBorder());
  69.       this._scpLeft.getViewport().setView(this._lstLeft);
  70.       AwtUtil.addComponent(this, this._scpLeft, gbl, gbc, 0, 1, 1, 4, (double)0.0F, 0.3);
  71.       gbc.fill = 2;
  72.       gbc.anchor = 15;
  73.       gbc.insets = new Insets(0, 3, 0, 3);
  74.       AwtUtil.addComponent(this, this._btnToRight, gbl, gbc, 1, 1, 1, 1, (double)0.0F, 0.3);
  75.       gbc.insets.bottom = 0;
  76.       this._lstRight.setFixedCellWidth(100);
  77.       gbc.fill = 1;
  78.       gbc.anchor = 10;
  79.       gbc.insets = new Insets(0, 0, 0, 0);
  80.       this._scpRight.setBorder(BorderFactory.createLoweredBevelBorder());
  81.       this._scpRight.getViewport().setView(this._lstRight);
  82.       AwtUtil.addComponent(this, this._scpRight, gbl, gbc, 2, 1, 1, 4, (double)1.0F, 0.3);
  83.       gbc.insets.top = 5;
  84.       gbc.fill = 2;
  85.       gbc.anchor = 10;
  86.       gbc.insets = new Insets(5, 3, 0, 3);
  87.       AwtUtil.addComponent(this, this._btnAllToRight, gbl, gbc, 1, 2, 1, 1, (double)0.0F, (double)0.0F);
  88.       gbc.insets.top = 5;
  89.       gbc.fill = 2;
  90.       gbc.anchor = 11;
  91.       gbc.insets = new Insets(5, 3, 0, 3);
  92.       AwtUtil.addComponent(this, this._btnAllToLeft, gbl, gbc, 1, 3, 1, 1, (double)0.0F, (double)0.0F);
  93.       gbc.insets.top = 5;
  94.       gbc.fill = 2;
  95.       gbc.anchor = 11;
  96.       gbc.insets = new Insets(5, 3, 0, 3);
  97.       AwtUtil.addComponent(this, this._btnToLeft, gbl, gbc, 1, 4, 1, 1, (double)0.0F, 0.3);
  98.    }
  99.  
  100.    protected void initComponentListeners() {
  101.       ActionListener l2lp = new L2LPActionListener(this);
  102.       this._btnToLeft.addActionListener(l2lp);
  103.       this._btnToRight.addActionListener(l2lp);
  104.       this._btnAllToLeft.addActionListener(l2lp);
  105.       this._btnAllToRight.addActionListener(l2lp);
  106.    }
  107.  
  108.    public JLabel getLeftListLabel() {
  109.       return this._lblLeftList;
  110.    }
  111.  
  112.    public JLabel getRightListLabel() {
  113.       return this._lblRightList;
  114.    }
  115.  
  116.    public JList getLeftList() {
  117.       return this._lstLeft;
  118.    }
  119.  
  120.    public JList getRightList() {
  121.       return this._lstRight;
  122.    }
  123.  
  124.    public JButton getToRightButton() {
  125.       return this._btnToRight;
  126.    }
  127.  
  128.    public JButton getToLeftButton() {
  129.       return this._btnToLeft;
  130.    }
  131.  
  132.    protected void moveToRight(boolean all) {
  133.       this.moveFromTo(this._lstLeft, this._lstRight, all);
  134.    }
  135.  
  136.    protected void moveToLeft(boolean all) {
  137.       this.moveFromTo(this._lstRight, this._lstLeft, all);
  138.    }
  139.  
  140.    protected void copyToRight(boolean all) {
  141.       this.copyFromTo(this._lstLeft, this._lstRight, all);
  142.    }
  143.  
  144.    protected void removeFromRight(boolean all) {
  145.       this.removeFrom(this._lstRight, all);
  146.    }
  147.  
  148.    protected void setMoveSrcItems(boolean set) {
  149.       this._moveSrcItems = set;
  150.    }
  151.  
  152.    protected void moveFromTo(JList lstSrc, JList lstDst, boolean all) {
  153.       int[] selIndices = null;
  154.       if (all) {
  155.          int numitems = lstSrc.getModel().getSize();
  156.          selIndices = new int[numitems];
  157.  
  158.          for(int i = 0; i < numitems; selIndices[i] = i++) {
  159.          }
  160.       } else {
  161.          selIndices = lstSrc.getSelectedIndices();
  162.       }
  163.  
  164.       if (selIndices != null && Array.getLength(selIndices) >= 1) {
  165.          DefaultListModel mdlSrc = (DefaultListModel)lstSrc.getModel();
  166.          DefaultListModel mdlDst = (DefaultListModel)lstDst.getModel();
  167.          int del = 0;
  168.  
  169.          for(int i = 0; i < Array.getLength(selIndices); ++i) {
  170.             Object item = mdlSrc.getElementAt(selIndices[i] - del);
  171.             if (this._xferItemListener != null) {
  172.                item = this._xferItemListener.getItemToXfer(item);
  173.             }
  174.  
  175.             if (item != null) {
  176.                mdlSrc.removeElementAt(selIndices[i] - del);
  177.                mdlDst.addElement(item);
  178.                ++del;
  179.             }
  180.          }
  181.  
  182.       }
  183.    }
  184.  
  185.    protected void copyFromTo(JList lstSrc, JList lstDst, boolean all) {
  186.       int[] selIndices = null;
  187.       if (all) {
  188.          int numitems = lstSrc.getModel().getSize();
  189.          selIndices = new int[numitems];
  190.  
  191.          for(int i = 0; i < numitems; selIndices[i] = i++) {
  192.          }
  193.       } else {
  194.          selIndices = lstSrc.getSelectedIndices();
  195.       }
  196.  
  197.       if (selIndices != null && Array.getLength(selIndices) >= 1) {
  198.          OrderedListModel mdlSrc = (OrderedListModel)lstSrc.getModel();
  199.          OrderedListModel mdlDst = (OrderedListModel)lstDst.getModel();
  200.  
  201.          for(int i = 0; i < Array.getLength(selIndices); ++i) {
  202.             Object srcItem = ((DefaultListModel)mdlSrc).getElementAt(selIndices[i]);
  203.             boolean found = false;
  204.             Enumeration e = ((DefaultListModel)mdlDst).elements();
  205.  
  206.             while(e.hasMoreElements()) {
  207.                Object dstItem = e.nextElement();
  208.                if (dstItem == srcItem) {
  209.                   found = true;
  210.                }
  211.             }
  212.  
  213.             if (!found) {
  214.                if (this._xferItemListener != null) {
  215.                   srcItem = this._xferItemListener.getItemToXfer(srcItem);
  216.                }
  217.  
  218.                if (srcItem != null) {
  219.                   mdlDst.addElement(srcItem);
  220.                }
  221.             }
  222.          }
  223.  
  224.       }
  225.    }
  226.  
  227.    protected void removeFrom(JList lst, boolean all) {
  228.       int[] selIndices = null;
  229.       if (all) {
  230.          int numitems = lst.getModel().getSize();
  231.          selIndices = new int[numitems];
  232.  
  233.          for(int i = 0; i < numitems; selIndices[i] = i++) {
  234.          }
  235.       } else {
  236.          selIndices = lst.getSelectedIndices();
  237.       }
  238.  
  239.       if (selIndices != null && Array.getLength(selIndices) >= 1) {
  240.          OrderedListModel mdl = (OrderedListModel)lst.getModel();
  241.  
  242.          for(int i = 0; i < Array.getLength(selIndices); ++i) {
  243.             ((DefaultListModel)mdl).getElementAt(selIndices[i] - i);
  244.             mdl.removeElementAt(selIndices[i] - i);
  245.          }
  246.  
  247.       }
  248.    }
  249.  
  250.    protected void handleListSelection(MouseEvent e) {
  251.       if (((ComponentEvent)e).getComponent() == this._lstLeft) {
  252.          this._btnToRight.setEnabled(true);
  253.       } else if (((ComponentEvent)e).getComponent() == this._lstRight) {
  254.          this._btnToLeft.setEnabled(true);
  255.       }
  256.  
  257.    }
  258.  
  259.    public void addListToListXferItemListener(ListToListXferItemListener l) throws TooManyListenersException {
  260.       if (this._xferItemListener != null) {
  261.          throw new TooManyListenersException("ListToTablePanel can only have one listener");
  262.       } else {
  263.          this._xferItemListener = l;
  264.       }
  265.    }
  266.  
  267.    public void removeListToListXferItemListener(ListToListXferItemListener l) {
  268.       this._xferItemListener = null;
  269.    }
  270.  
  271.    public void setLeftListModel(ListModel listModel) {
  272.       if (listModel != null) {
  273.          this._lstLeft.setModel(listModel);
  274.          this._lstLeft.invalidate();
  275.          Container rootContainer = this._lstLeft.getTopLevelAncestor();
  276.          if (rootContainer != null) {
  277.             rootContainer.validate();
  278.          }
  279.  
  280.          if (listModel.getSize() > 0) {
  281.             this._lstLeft.setSelectedIndex(0);
  282.          }
  283.       }
  284.  
  285.    }
  286.  
  287.    // $FF: synthetic method
  288.    static JButton access$0(ListToListPanel $0) {
  289.       return $0._btnToLeft;
  290.    }
  291.  
  292.    // $FF: synthetic method
  293.    static JButton access$1(ListToListPanel $0) {
  294.       return $0._btnAllToLeft;
  295.    }
  296.  
  297.    // $FF: synthetic method
  298.    static boolean access$2(ListToListPanel $0) {
  299.       return $0._moveSrcItems;
  300.    }
  301.  
  302.    // $FF: synthetic method
  303.    static JButton access$3(ListToListPanel $0) {
  304.       return $0._btnToRight;
  305.    }
  306.  
  307.    // $FF: synthetic method
  308.    static JButton access$4(ListToListPanel $0) {
  309.       return $0._btnAllToRight;
  310.    }
  311. }
  312.